﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;

namespace Movies
{
    public partial class FormDisplayMovieDetails : Form
    {
        private MovieDetails details;
        public FormDisplayMovieDetails(MovieDetails detailsPased)
        {
            InitializeComponent();
            details = detailsPased;
        }

        private void DisplayDetails_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = details.Name;
                titleText.Text = details.Name;
                validateAndLoad(details.ImdbRating, rattingText, rattingLabel);


                double rateWidthValue = 1;
                try
                {
                    if (details.ImdbRating != null && !details.ImdbRating.Trim().Equals("N/A") && !details.ImdbRating.Trim().Equals(""))
                    {
                        rateWidthValue = Convert.ToDouble(details.ImdbRating);
                    }
                }
                catch (Exception ex)
                { 
                    HelperContentProvider.OmitWarning(ex); 
                }
                rateWidthValue = (rateWidthValue - 1) * 25;
                ratingValue.Width = (int)rateWidthValue;

                validateAndLoad(details.ImdbVotes, voteText, voteLabel);
                validateAndLoad(details.Rated, ratedText, ratedLabel);

                validateAndLoad(details.Released, releaseText, releseLabel);
                validateAndLoad(details.Runtime, runtimeText, runtimeLabel);
                validateAndLoad(details.GenreList, generText, genreLabel);
                validateAndLoad(details.Plot, plotText, plotLabel);

                validateAndLoad(details.Director, directorText, directorLabel);
                validateAndLoad(details.Actors, castText, castLabel);
                validateAndLoad(details.Writer, writerText, WriterLabel);
                validateAndLoad(details.Metascore, MetascoreText, Metascore);
                validateAndLoad(details.Award, AwardText, Award);

                isAlreadySeen.Checked = details.IsAlreadySeen;

            }
            catch (Exception ex)
            {
                HelperContentProvider.OmitWarning(ex);
            }

            try
            {
                Thread t = new Thread(delegate()
                {
                    loadImage();
                });
                t.Start();
            }
            catch (Exception ex)
            {
                HelperContentProvider.OmitWarning(ex);
            }
        }

        private void loadImage()
        {
            String imageFile = details.LocalCoverLocation;
            if (details.LocalCoverLocation != null)
            {
                imageFile = imageFile.Replace("file:\\\\", "");
                if (File.Exists(imageFile) && !imageFile.ToUpper().Trim().Equals("N/A") && !imageFile.ToUpper().Trim().Equals(""))
                {
                    try
                    {
                        Stream newFile = new FileStream(imageFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                        Bitmap fileBMP = new Bitmap(newFile);
                        newFile.Close();
                        poster.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
                        poster.BackgroundImage = fileBMP; //(System.Drawing.Image)(resources.GetStream());
                        fileBMP = null;
                    }
                    catch (Exception ex)
                    { HelperContentProvider.OmitWarning(ex); }
                }
            }

        }

        private void validateAndLoad(String data, Control textControl, Control labelControl)
        {
            if (data != null && !data.Trim().Equals(""))
            {
                textControl.Text = data;
            }
            else
            {
                textControl.Hide();
                labelControl.Hide();
            }
        }

        private void doneButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (details.LoadLocation != null && !details.LoadLocation.Trim().Equals(""))
                {
                    FormShowMovies playForm = new FormShowMovies(details.LoadLocation, false);
                    DialogResult result = playForm.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        isAlreadySeen.Checked = true;
                        this.Dispose();
                    }
                }
                //this.Dispose();
            }
            catch (Exception ex)
            {
                HelperContentProvider.OmitWarning(ex);
            }
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            if (details.LoadLocation != null && !details.LoadLocation.Trim().Equals(""))
            {
                try
                {
                    Process.Start(@details.LoadLocation);
                }
                catch (Exception ex)
                {
                    HelperContentProvider.OmitWarning(ex);
                }
            }
        }

        private void userRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            details.UserRatting = userRate.Text;
        }

        private void FormDisplayMovieDetails_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (isAlreadySeen.Checked != details.IsAlreadySeen)
            {
                details.IsAlreadySeen = isAlreadySeen.Checked;
                details.SaveUserDetails();            
            }

        }
    }
}
